#include "GrapChart.h"

#include <QBarCategoryAxis>
#include <QBarSet>

BarChart::BarChart(QWidget *parent) : Chart(parent)
{
    m_series.reset(new QBarSeries(this));


    //根据网络数据创建图表
    QBarSet bar0("bar1");

    m_series->append(&bar0);

    //创建x ，y轴坐标
    QBarCategoryAxis *xaxis=new QBarCategoryAxis(this);
    xaxis->append("");
    m_grch->addAxis(xaxis,Qt::AlignBottom);
    m_series->attachAxis(xaxis);

    QBarCategoryAxis *yaxis=new QBarCategoryAxis(this);
    yaxis->append("");
    m_grch->addAxis(yaxis,Qt::AlignLeft);
    m_series->attachAxis(yaxis);

    //设置图表
    m_grch->setAnimationOptions(QChart::SeriesAnimations);//设置为动态
    m_grch->addSeries(m_series.get());

    m_view->setRenderHint(QPainter::Antialiasing);//设置抗锯齿
}

QChartView *BarChart::DisPlay()
{
    QChartView *rt= m_view.get();
    m_view.reset();
    return rt;
}


LineChar::LineChar(QWidget *parent) :Chart(parent)
{
    m_series.reset(new QLineSeries(this));


    m_series->append(0, 6);
    m_series->append(2, 4);
    m_series->append(3, 8);
    m_series->append(7, 4);
    m_series->append(10, 5);


    m_grch->addSeries(m_series.get());          // 添加数据系列
    m_grch->setTitle("折线图");     // 设置标题
    m_grch->createDefaultAxes();       // 创建默认坐标轴
    m_grch->setAnimationOptions(QChart::SeriesAnimations);

    // 创建视图（QChartView）

    m_view->setRenderHint(QPainter::Antialiasing); // 启用抗锯齿

}

QChartView *LineChar::DisPlay()
{

    auto rt =m_view.get();

    return  rt;
}

void LineChar::setChartData(int x, int y)
{
    m_series->remove(0);
    m_series->append(x,y);
}

SplineChar::SplineChar(QWidget *parent) : Chart(parent)
{
    m_series.reset(new QSplineSeries(this));

    m_series->append(1,23);
    m_series->append(2,13);
    m_series->append(3,12);
    m_series->append(4,43);
    m_series->append(5,23);

    m_grch->addSeries(m_series.get());          // 添加数据系列
    m_grch->setTitle("曲线图");     // 设置标题
    m_grch->createDefaultAxes();       // 创建默认坐标轴
    m_grch->setAnimationOptions(QChart::SeriesAnimations);

    m_view->setRenderHint(QPainter::Antialiasing); // 启用抗锯齿
}

QChartView *SplineChar::DisPlay()
{
    auto it =m_view.get();
    return it;
}

void SplineChar::setChartData(int x, int y)
{
    m_series->remove(0);

    m_series->append(x,y);
}

Chart::Chart(QWidget *parent) : QWidget(parent)
{
       m_grch =std::make_shared<QChart>();
       m_view =std::make_shared<QChartView>(m_grch.get());
}
